<template>
  <div class="layui-container fly-marginTop">
    <div class="fly-panel fly-panel-user" pad20>
      <div class="layui-tab layui-tab-brief" lay-filter="user">
        <ul class="layui-tab-title">
          <li>
            <router-link to="/login">登入</router-link>
          </li>
          <li class="layui-this">注册</li>
        </ul>
        <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
          <div class="layui-tab-item layui-show">
            <validation-observer ref="observer" v-slot="{validate}">
            <div class="layui-form layui-form-pane">
              <form method="post">
                <div class="layui-form-item">
                  <label for="L_email" class="layui-form-label">邮箱</label>
                  <validation-provider name="email" rules="required|email" v-slot="{errors}">
                    <div class="layui-input-inline">
                      <input type="text" v-model="email" name="email" placeholder="请输入你的邮箱" autocomplete="off"
                        class="layui-input" />
                    </div>
                    <div class="error layui-form-mid">
                      <span style="color: #c00">{{ errors[0] }}</span>
                    </div>
                  </validation-provider>
                </div>
                <div class="layui-form-item">
                  <label for="nickname" class="layui-form-label">昵称</label>
                  <validation-provider name="nickname" rules="required|min:4" v-slot="{errors}">
                  <div class="layui-input-inline">
                    <input
                      type="text"
                      name="nickname"
                      v-model="nickname"
                      placeholder="请输入昵称"
                      autocomplete="off"
                      class="layui-input"
                    />
                  </div>
                  <div class="error layui-form-mid">
                      <span style="color: #c00">{{ errors[0] }}</span>
                    </div>
                  </validation-provider>
                </div>
                <div class="layui-form-item">
                  <label for="L_pass" class="layui-form-label">密码</label>
                  <validation-provider rules="required|min:6|max:16" v-slot="{errors}" name="password">
                    <div class="layui-input-inline">
                      <input type="password" v-model="password" name="password" placeholder="请输入你的密码" autocomplete="off"
                        class="layui-input" />
                    </div>
                    <div class="error layui-form-mid">
                      <span style="color: #c00">{{ errors[0] }}</span>
                    </div>
                  </validation-provider>
                </div>
                <div class="layui-form-item">
                  <label for="L_repass" class="layui-form-label">确认密码</label>
                  <validation-provider name="repass" rules="required|confirmed:password" v-slot="{errors}">
                    <div class="layui-input-inline">
                      <input type="password" v-model="repass" name="repass" placeholder="请输入确认密码" autocomplete="off"
                        class="layui-input" />
                    </div>
                    <div class="error layui-form-mid">
                      <span style="color: #c00">{{ errors[0] }}</span>
                    </div>
                  </validation-provider>
                </div>
                <div class="layui-form-item">
                  <validation-provider name="code" rules="required|length:4" v-slot="{errors}">
                    <div class="layui-row">
                      <label for="L_vercode" class="layui-form-label">验证码</label>
                      <div class="layui-input-inline">
                        <input type="text" v-model="code" name="code" placeholder="请输入验证码" autocomplete="off" class="layui-input" />
                      </div>
                      <div class>
                        <span style="color: #c00;" @click="_getCode()" class="svg" v-html="svg"></span>
                      </div>
                    </div>
                    <div class="error layui-form-mid">
                      <span style="color: #c00">{{ errors[0] }}</span>
                    </div>
                  </validation-provider>
                </div>
                <div class="layui-form-item">
                  <button class="layui-btn" type="button"  @click="validate().then(submit)">立即注册</button>
                </div>
                <div class="layui-form-item fly-form-app">
                  <span>或者直接使用社交账号快捷注册</span>
                  <a href onclick="layer.msg('正在通过QQ登入', {icon:16, shade: 0.1, time:0})" class="iconfont icon-qq"
                    title="QQ登入"></a>
                  <a href onclick="layer.msg('正在通过微博登入', {icon:16, shade: 0.1, time:0})" class="iconfont icon-weibo"
                    title="微博登入"></a>
                </div>
              </form>
            </div>
            </validation-observer>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ValidationProvider, ValidationObserver } from 'vee-validate'
import { getCode, regist } from '@/api/login'

export default {
  name: 'regist',
  data () {
    return {
      email: '',
      nickname: '',
      password: '',
      repass: '',
      code: '',
      svg: ''
    }
  },
  components: {
    ValidationProvider, ValidationObserver
  },
  mounted () {
    this._getCode()
  },
  methods: {
    _getCode () {
      const sid = this.$store.state.sid
      getCode(sid).then((res) => {
        if (res.code === 200) { this.svg = res.data }
      })
    },
    async submit () {
      const isValid = await this.$refs.observer.validate()
      if (!isValid) {
        return
      }

      regist({
        email: this.email,
        nickname: this.nickname,
        password: this.password,
        code: this.code,
        sid: this.$store.state.sid
      }).then((res) => {
        if (res.code === 200) {
          console.log(res)
          this.username = ''
          this.nickname = ''
          this.password = ''
          this.code = ''
          requestAnimationFrame(() => {
            this.$refs.observer.reset()
          })
          this.$alert('注册成功')
          setTimeout(() => {
            this.$router.push('/login')
          }, 1000)
        } else {
          // res.msg => {username:[],nickname:[],code:[]}
          // this.$refs.observer.setErrors({
          //   code: [res.msg]
          // })
          this.$refs.observer.setErrors(res.msg)
        }
      }).catch((err) => {
        const data = err.response.data
        if (data.code === 500) {
          this.$alert('用户名不存在，请重新输入')
        } else {
          this.$alert('服务器链接错误')
        }
        console.log(err)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
